package com.ctakit.proxy.asm;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class LogMethodVisitor extends MethodVisitor
{
	public LogMethodVisitor(MethodVisitor mv)
	{
		super(Opcodes.ASM5, mv);
	}

	public void visitCode() {
		/**
		 * 方法执行之前植入代码
		 */
		//super.visitMethodInsn(Opcodes.INVOKESTATIC, "com/ctakit/proxy/asm/Log", "startLog", "()V", false);
		
		super.visitVarInsn(Opcodes.ALOAD, 0);
		super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
		super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
		super.visitMethodInsn(Opcodes.INVOKESTATIC, "com/ctakit/proxy/asm/Log", "startLog", "(Ljava/lang/String;)V", false);
		super.visitCode();
	}
	
	public void visitInsn(int opcode)
	{
		if(opcode == Opcodes.RETURN)
		{
			/**
			 * 方法return之前，植入代码
			 */
			super.visitMethodInsn(Opcodes.INVOKESTATIC, "com/ctakit/proxy/asm/Log", "endLog", "()V", false);
		}
		super.visitInsn(opcode);
	}
}